﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using FreeGoo.GameEngine;
using FreeGoo.GameEngine.Renderables;

namespace FreeGoo.LevelClasses.LevelParts
{
    public abstract class BodyRenderableLevelPart : RenderableLevelPart
    {
        protected const float DefaultFrictionCoefficient = 1.0f;
        protected const float DefaultRestitutionCoefficient = 0.2f;

        public override Renderable BuildRenderable(Level level, XElement element)
        {
            BodyRenderable bodyRenderable = BuildBodyRenderable(level, element);
            UpdateProperties(level, element, bodyRenderable);
            UpdateSurface(level, element, bodyRenderable);
            return bodyRenderable;
        }

        public abstract BodyRenderable BuildBodyRenderable(Level level, XElement element);        

        protected void UpdateSurface(Level level, XElement element, BodyRenderable bodyRenderable)
        {
            bodyRenderable.Geom.FrictionCoefficient = element.Attribute("FrictionCoefficient").ToFloat(DefaultFrictionCoefficient);
            bodyRenderable.Geom.RestitutionCoefficient = element.Attribute("RestitutionCoefficient").ToFloat(DefaultRestitutionCoefficient);
            bodyRenderable.Geom.CollisionEnabled = element.Attribute("CollisionEnabled").ToBool(true);
            bodyRenderable.IsDeadly = element.Attribute("IsDeadly").ToBool();
        }
    
        private void UpdateProperties(Level level, XElement element, BodyRenderable bodyRenderable)
        {
            if (element.Attribute("IsStatic").ToBool())
            {
                bodyRenderable.Body.IsStatic = true;
            }
        }
    }
}
